﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;

namespace eproject_BusTicketOnline
{
    public partial class TicketManage : System.Web.UI.Page
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Panel1.Visible = false;
                SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["ApplicationServices"].ToString());
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter("SELECT     dbo.BusticKetDetail.BusTicketDetailID AS [Ticket ID], dbo.BusticKetDetail.BusListId AS [Bus ID], dbo.Member.FullName AS Payer,  dbo.BusticKetDetail.FullName AS Passenger, dbo.BusticKetDetail.Age, dbo.BusticKetDetail.DeparturePlace, dbo.BusticKetDetail.ArrivePlace,  dbo.BusticKetDetail.StartDate, dbo.BusticKetDetail.EndDate, dbo.BusticKetDetail.Time, dbo.BusticKetDetail.Price, dbo.BusticKetDetail.TicketStatus FROM         dbo.Member INNER JOIN    dbo.BusticKetDetail ON dbo.Member.MemberId = dbo.BusticKetDetail.MemberId ", con);
                DataSet ds = new DataSet();
                da.Fill(ds);
                GridView1.DataSource = ds;
                GridView1.DataBind();
            }
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Session["TicketID"] = GridView1.SelectedRow.Cells[1].Text;
            Response.Redirect("EditTicket.aspx");
        }

        protected void btnAddTicket_Click(object sender, EventArgs e)
        {
            Response.Redirect("CreateTicket.aspx");
        }

        protected void btnDelTicket_Click(object sender, EventArgs e)
        {
            Panel1.Visible = true;
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["ApplicationServices"].ToString());
                con.Open();
                SqlCommand cmd = new SqlCommand("DELETE FROM BusticketDetail where BusticketDetailId='" + txtDelTicket.Text + "'", con);
                cmd.ExecuteNonQuery();
                Response.Redirect("TicketManage.aspx");
            }
            catch
            {
                lblDel.Text = "ID invalid !!! ";
            }
        }
    }
}